<?php
require_once('config.php');
require_once('database.php');
require_once('user.php');

class Page{

	public $logged;
	function __construct(){
		$this->is_logged_in();
	}
	
	public function login(){

	}
	
	public function signup(){
		if(isset($_POST['signup'])){
			if(!empty($_POST['username']) || !empty($_POST['password'])){
				if($_POST['password'] == $_POST['password2']){
					//check if username already exists
					$user = User::find_by_username($_POST['username']);
					
					if($user->username != $_POST['username']){
					
						$user->insert('portal', $_POST['username'], $_POST['password']);
						header("location: " . $config['base_url'] . "index.php");
						exit();
							
					} else {
						echo "username already exists";
					}
				}	
			}
		}
	}

	public function update_form(){
			if(isset($_POST['update'])){		
				if($update_user){
					header("Location: " . $config['base_url'] . "index.php?action=read");
					exit();
				}
			}
		$fields = array();
		$fields  = $_POST;
		$user = User::find_by_id($_GET['id']);
		$user->id = $_GET['id'];
		$update_user = $user->update($user->id, $fields);
	
		include_once 'template/forms/update_form.php';
	
	
	}
	
	public function is_logged_in(){	
			if(!$this->logged){	
				$this->login();
			}
	}
}
$page = new Page();
